<template>
  <div ref="dragbar" class="dragbar">
    <drag @move="onMove" dir="x" :rect="rect">
      <div class="dragbar-box"></div>
    </drag>
  </div>
</template>

<script>
import Drag from '~/components/Drag'
export default {
  components: {
    Drag
  },
  data() {
    return {
      rect: [-1, -1, 0, 0]
    }
  },
  mounted() {
    // 获取滚动条最大移动的距离  盒边框(滚动条宽度) - 拖拽元素宽度
    // console.log(this.$refs.dragbar.offsetWidth)
    this.rect[3] = this.$refs.dragbar.offsetWidth - 100
  },
  methods: {
    onMove(pos) {
      // pos参数: 从子组件里发送过来的值
      // 计算 拖拽元素 相对于滚动条 移动的百分比 并发送
      this.$emit("value", parseInt((pos.left / this.rect[3]) * 100 ))
    }
  }
}
</script>

<style lang="scss">
  .dragbar {
    position: relative;
    height: 3px;
    width: 100%;
    background: #aaa;
    &-box {
      width: 100px;
      height: 9px;
      margin-top: -3px;
      border-radius: 5px;
      background: #888;
    }
  }
</style>